package com.centit.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.model.basedata.IUnitInfo;
import com.centit.framework.model.basedata.IUserInfo;
import com.centit.framework.security.model.CentitUserDetails;

@Controller
@RequestMapping("/ztree")
public class ZtreeUtil extends BaseController {
	private static final Log log = LogFactory.getLog(ZtreeUtil.class);
	
	
	
	  @RequestMapping(value = "/aa", method = {RequestMethod.GET})
	    public void aa(HttpServletRequest request, HttpServletResponse response) {
		  CentitUserDetails loginUser = getLoginUser(request);
		  System.out.println(loginUser.getUserInfo().getUserName());
	  }
	
	
	
	   @RequestMapping(value = "/ztreeInfo", method = {RequestMethod.GET})
	    public void ztreeEdit(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
			JSONArray array=new JSONArray();
			String type=request.getParameter("type");
			if(StringUtils.isNotBlank(type)){
				if("users".equals(type)){  //获取事项树结构
					List<IUserInfo> userList=CodeRepositoryUtil.getAllUsers("T");
					userList.removeIf(u->"U00001".equals(u.getPrimaryUnit()));
					List<IUnitInfo> unitList=CodeRepositoryUtil.getAllUnits("T");
					unitList.removeIf(u->"U00001".equals(u.getUnitCode()));
					for(int i=0;i<unitList.size();i++){
						IUnitInfo unit=unitList.get(i);
						JSONObject obj=new JSONObject();
						obj.put("id", "P"+unit.getUnitCode());
	    				obj.put("name", unit.getUnitName());
	    				obj.put("pid", "0");
	    				obj.put("orderno", unit.getUnitOrder());
	    				obj.put("nocheck", "true");
	    				array.add(obj);
					}
					for(int i=0;i<userList.size();i++){
						IUserInfo user=userList.get(i);
						JSONObject obj=new JSONObject();
						obj.put("id", user.getUserCode());
	    				obj.put("name", user.getUserName());
	    				obj.put("pid", "P"+user.getPrimaryUnit());
	    				obj.put("orderno", user.getUserOrder());
	    				array.add(obj);
					}
				}
			    //对用户数组进行排序
	            array = sortJSONArrayNew(array);
	    		request.setAttribute("selectID", array);
	    		request.setAttribute("type", type);
	    		request.setAttribute("chkType", request.getParameter("chkType")); //checkbox 为多选    radio 为单选
	    		request.setAttribute("Code", request.getParameter("value"));//保存 key  的hidden 
	    		request.setAttribute("Name", request.getParameter("name"));//保存name  的text
				request.getRequestDispatcher("/modules/util/zcTreeNew.jsp").forward(request, response); 
			}
	   }
	   
	   
	   public static JSONArray sortJSONArrayNew(JSONArray jsonArr){
	        for (int i = 0; i < jsonArr.size(); i++) {
	        	if(jsonArr.getJSONObject(i).get("orderno")==null||StringUtils.isBlank(jsonArr.getJSONObject(i).get("orderno").toString())){
	        		continue;
	        	}
	            long l = Long.parseLong(jsonArr.getJSONObject(i).get("orderno").toString());
	            for (int j = i + 1; j < jsonArr.size(); j++) {
	            	if(jsonArr.getJSONObject(j).get("orderno")==null||StringUtils.isBlank(jsonArr.getJSONObject(j).get("orderno").toString())){
	            		continue;
	            	}
	                long nl = Long.parseLong(jsonArr.getJSONObject(j).get("orderno").toString());
	                if (l > nl) {
	                    JSONObject temp = jsonArr.getJSONObject(j);
	                    l = nl;
	                    jsonArr.set(j, jsonArr.getJSONObject(i));
	                    jsonArr.set(i, temp);
	                }
	            }
	        }
	        return jsonArr;
	    }
}
